package cn.huihuo.weibo.service;

import cn.huihuo.weibo.bean.hot.HotBean;
import cn.huihuo.weibo.bean.hot.HotResultBean;
import cn.huihuo.weibo.module.Comment;
import cn.huihuo.weibo.module.Hot;
import cn.huihuo.weibo.module.ProdComment;
import cn.huihuo.weibo.module.Product;
import cn.huihuo.weibo.utils.TimeUtils;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;

/**
 * 热点信息整合
 * @author Dan
 * Created by zhengjunbo on 2015/10/30.
 */
public class HotService {
    public static final HotService service = new HotService();
    public HotResultBean findWhatsHot(Integer pageNumber,Integer count){
        HotResultBean hotResultBean = new HotResultBean();
        hotResultBean.setHotBeans(new ArrayList<>());


        Page<Product> allHot = Product.product.findAllHot(pageNumber, count);
        hotResultBean.setTotalSize(String.valueOf(allHot.getTotalPage()));
        for(Product prodComment:allHot.getList()){
            HotBean hotBean = new HotBean();
            Integer prodId = prodComment.getInt("prodId");
            hotBean.setProdPicUrl(prodComment.getStr("inner_pic_path")==null?prodComment.getStr("out_pic_path"):prodComment.getStr("inner_pic_path")); //这里需要变成哪里有换哪里
            hotBean.setInteractive(prodComment.getInt("interaction")+"次互动");
            hotBean.setParticipate(prodComment.getInt("interaction_user")+"参与");
            hotBean.setProdTitle(prodComment.getStr("prod_title"));
            hotBean.setProdUrl("/product/"+prodComment.getInt("prodId"));


            ProdComment byProdIdNew = ProdComment.prodComment.findByProdIdNew(prodId);

            if(byProdIdNew==null){
                continue;
            }
            hotBean.setTypeDescription(getTypeDescription(byProdIdNew.getStr("ugc_type")));
            hotBean.setComment(byProdIdNew.get("product_comment"));
            hotBean.setTime(TimeUtils.getTime(byProdIdNew.getTimestamp("comm_time").getTime()));
            hotBean.setUserName(byProdIdNew.getStr("nickname"));
            hotBean.setUserAvatar(byProdIdNew.getStr("avatar"));
            hotBean.setUserUrl("/user/" + byProdIdNew.getStr("userId"));
            hotBean.setUrl(byProdIdNew.getStr("url"));
            hotResultBean.getHotBeans().add(hotBean);
        }

        return hotResultBean;
    }

    private String getTypeDescription(String ugcType) {
        if("2".equals(ugcType)){
            return "# 讨论";
        } else if("3".equals(ugcType)){
            return "# 摘抄";
        } else if("4".equals(ugcType)){
            return "# 购买渠道";
        }
        return "";
    }


    /**
     * 每次添加成功的时候去更新即可。
     * @param ugcId
     */
    public void ugcHot(Integer ugcId){
        Hot hot = Hot.hot.findHotByUgcId(ugcId);
        if(hot == null){
            hot = new Hot();
            hot.set("ugcId",ugcId).save();
        }

        Comment commentSize = Comment.comment.getCommentSize(ugcId);
        Comment commUserSize = Comment.comment.getCommUserSize(ugcId);

        hot.set("interaction",commentSize.getLong("size")).set("total_user", commUserSize.getLong("size")).update();
    }
}
